package com.ruoyi.gbase.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gbase.domain.GbaseJobInfo;
import com.ruoyi.gbase.service.IGbaseJobInfoService;
import com.ruoyi.gbase.util.LlUtil;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 拉链表Controller
 * 
 * @author yys
 * @date 2021-11-15
 */
@RestController
@RequestMapping("/llb")
public class TLLBController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(TLLBController.class);
  
    @Autowired
    private IGbaseJobInfoService gbaseJobInfoService;

    @Autowired
    private LlUtil llUtil;

    /**
     * 批量从头跑拉链
     */
    @Log(title = "批量从头跑拉链")
    @PostMapping( "/initALl")
    @ResponseBody
    public AjaxResult initALl(String ids)
    {
        String[] ids2 = Convert.toStrArray(ids);
        GbaseJobInfo gbaseJobInfo;
        for(String jobid:ids2){
            gbaseJobInfo = gbaseJobInfoService.selectGbaseJobInfoById(jobid);
            if(gbaseJobInfo.getSendCondition().equals("0")){
                llUtil.initQLll("20181231",gbaseJobInfo.getSchemaName(),gbaseJobInfo.getTableName());
            }else if(gbaseJobInfo.getSendCondition().equals("1")){
                llUtil.initZBLll("20181231",gbaseJobInfo.getSchemaName(),gbaseJobInfo.getTableName());
            }
        }

        return success("后台开始跑数据了");
    }

    /**
     * 删除拉链表   将这些表的 iill改为0
     */
    @Log(title = "删除拉链表")
    @PostMapping( "/deleteLLALl")
    @ResponseBody
    public AjaxResult deleteLLALl(String ids)
    {
        String[] ids2 = Convert.toStrArray(ids);
        GbaseJobInfo gbaseJobInfo;
        for(String jobid:ids2){
            gbaseJobInfo = gbaseJobInfoService.selectGbaseJobInfoById(jobid);
            gbaseJobInfo.setIsll("0");
            gbaseJobInfo.setIsinc("0");
            gbaseJobInfoService.updateGbaseJobInfo(gbaseJobInfo);
        }

        return success("全部取消拉链成功");
    }

    /**
     * 这个方法是完全从日期开始跑，全量的拉链表
     * @param hisdate  开始日期 8位或者10位
     * @param schemaName  模式
     * @param tableName 表名
     * @return
     * @throws SchedulerException
     */
    @PostMapping("/initZBLll")
    @ResponseBody
    public AjaxResult initZBLll(String hisdate, String schemaName, String tableName)  throws SchedulerException
    {
        if(hisdate.length()<8){
            return error("日期格式不对");
        }
        hisdate = hisdate.replace("-","");
        log.info("成功到后台，开始异步跑增变量："+hisdate+" "+schemaName+"."+tableName);
        llUtil.initZBLll(hisdate,schemaName,tableName);
        return success();
    }

    /**
     * 这个方法是完全从日期开始跑，增量的拉链表
     * @param hisdate  开始日期 8位或者10位
     * @param schemaName  模式
     * @param tableName 表名
     * @return
     * @throws SchedulerException
     */
    @PostMapping("/initQLll")
    @ResponseBody
    public AjaxResult initQLll(String hisdate, String schemaName, String tableName)  throws SchedulerException
    {
        if(hisdate.length()<8){
            return error("日期格式不对");
        }
        hisdate = hisdate.replace("-","");
        log.info("成功到后台，开始异步跑全量："+hisdate+" "+schemaName+"."+tableName);
        llUtil.initQLll(hisdate,schemaName,tableName);
        return success();
    }


    /**
     * 从
     * @param hisdate  开始日期 8位或者10位
     * @param schemaName  模式
     * @param tableName 表名
     * @return
     * @throws SchedulerException
     */
    @ResponseBody
    @GetMapping("/callLl/{hisdate}/{schemaName}/{tableName}")
    public void callLl(@PathVariable(value = "hisdate", required = true) String hisdate,
                       @PathVariable(value = "schemaName", required = true) String schemaName,
                       @PathVariable(value = "tableName", required = true) String tableName)  throws SchedulerException
    {
        if(hisdate.length()<8){
        }
        hisdate = hisdate.replace("-","");
        log.info("成功到后台，开始异步跑全量："+hisdate+" "+schemaName+"."+tableName);
        llUtil.initQLll_zbl(hisdate,schemaName,tableName);
    }


    /**
     *  参数2个
     *  模式名称        schemaName
     *  表名           tableName
     *  拉链表已经正常跑的情况下 跑最新的拉链数据  最近
     */
    @ResponseBody
    @GetMapping("/lastLl/{schemaName}/{tableName}")
    public void lastLl(@PathVariable(value = "schemaName", required = true) String schemaName,
                       @PathVariable(value = "tableName", required = true) String tableName) {
        llUtil.lastLl(schemaName,tableName);
    }

    /**
     * 自动跑所有的拉链表数据
     */
    @GetMapping("/autoAll")
    public void autoAll()  {
        llUtil.autoRunLL();
    }




}